# 异常点处理
import pandas as pd
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import MinMaxScaler

# 假设df是您的DataFrame
df = pd.read_csv('no_neg.csv')

# 分离特征和标签
X = df.drop('Air Quality', axis=1)
y = df['Air Quality']

# 特征归一化
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)

# 应用DBSCAN算法
db = DBSCAN(eps=0.4, min_samples=200).fit(X_scaled)
'''
结果
eps,min_samples,num
0.5,100,12
0.5,200,18
0.4,100,111
0.4,200,205
0.4,300,302
0.35,200,529
0.35,100,311
0.3,100,820
'''

labels = db.labels_

# 识别离群点
outliers = labels == -1

# 打印离群点数量
print("离群点数量：", outliers.sum())

# 删除离群点
if not outliers.all():  # 检查是否有非离群点
    # 还原数据到原始尺度
    tmp = scaler.inverse_transform(X_scaled)
    # 合并
    tmp = pd.DataFrame(tmp, columns=X.columns)
    tmp['Air Quality'] = y
    # 去除异常点
    df_no_outliers = tmp[~outliers]
    # 输出
    df_no_outliers.info()
    print(df_no_outliers.describe())
    df_no_outliers.to_csv('no_neg_DBSCAN.csv', index=False)

else:
    print("所有数据点都被识别为离群点。")
    exit(0)